-module(p5).
-export([result/0, main/1]).

result() -> main(20).

main(Bound) -> least_common_multiple(lists:seq(2, Bound)).

least_common_multiple([F1, F2]) ->
    erlang:trunc(F1 * F2 / greatest_common_divisor(F1, F2));
least_common_multiple([F1, F2 | T]) ->
    First = least_common_multiple([F1, F2]), 
    least_common_multiple([First | T]).

greatest_common_divisor(F1, F2) when F2 > F1 ->
    greatest_common_divisor(F2, F1);
greatest_common_divisor(F1, F2) when F1 rem F2 =:= 0 -> F2;
greatest_common_divisor(F1, F2) ->
    greatest_common_divisor(F2, F1 rem F2).
